using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;	   

namespace Possan.Graphics.Filters
{						  
	public class Rotate : IBaseFilter
	{
		public string ParameterDefinition
		{
			get
			{
				     return null;
			}
		}

		private float _rot;

		public float Rotation
		{
			get
			{
				     return _rot;
			}
			set
			{
				_rot = value;
			}
		}

		public Rotate()
		{
			Rotation = 0;
		}
					   
		public Bitmap Filter(Bitmap oldbitmap)
		{
			//	Bitmap neu = bitmap.Clone() as Bitmap;

			//	Graphics g = Graphics.FromImage(neu);

			PointF[] tmp = new PointF[4];

			float ow = (float)oldbitmap.Width;
			float oh = (float)oldbitmap.Height;

			tmp[0].X = 0;
			tmp[0].Y = 0;

			tmp[1].X = ow;
			tmp[1].Y = 0;

			tmp[2].X = ow;
			tmp[2].Y = oh;

			tmp[3].X = 0;
			tmp[3].Y = oh;

			Logger.Debug(this.GetType(), "Original bounding box: " + ow + " x " + oh);
			for (int j = 0; j < 4; j++)
				Logger.Debug(this.GetType(), "  pt[" + j + "] = " + tmp[j].X + ", " + tmp[j].Y);

			Logger.Debug(this.GetType(), "Rotation = " + Rotation);

			Matrix mtx = new Matrix();
			mtx.Rotate(Rotation);
			mtx.TransformPoints(tmp);

			float minx = 1000000.0f;
			float miny = 1000000.0f;
			float maxx = 0.0f;
			float maxy = 0.0f;

			for (int j = 0; j < 4 ;  j++)
			{
				if (minx > tmp[j].X) minx = tmp[j].X;
				if (miny > tmp[j].Y) miny = tmp[j].Y;
				if (maxx < tmp[j].X) maxx = tmp[j].X;
				if (maxy < tmp[j].Y) maxy = tmp[j].Y;
			}

			int nw = (int)Math.Ceiling(maxx - minx);
			int nh = (int)Math.Ceiling(maxy - miny);

			Logger.Debug(this.GetType(), "Rotated bounding box: " + nw + " x " + nh);

			Logger.Debug(this.GetType(), "Rotated coords:");
			for (int j = 0; j < 4; j++)
				Logger.Debug(this.GetType(), "  pt[" + j + "] = " + tmp[j].X + ", " + tmp[j].Y);

			// uppsampla lite efterssom den ser ful ut annars

			Bitmap neu = new Bitmap(nw * 5, nh * 5);
			neu.SetResolution(oldbitmap.HorizontalResolution, oldbitmap.VerticalResolution);

			System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(neu);
			g.InterpolationMode = InterpolationMode.HighQualityBicubic;
			g.CompositingQuality = CompositingQuality.HighQuality;
			g.SmoothingMode = SmoothingMode.HighQuality;
			g.ResetTransform();
			g.ScaleTransform(5, 5);
			g.TranslateTransform(-minx, -miny);
			g.RotateTransform(Rotation);
			g.DrawImage(oldbitmap, 0, 0);

			Bitmap neu2 = new Bitmap(nw, nh);
			neu2.SetResolution(oldbitmap.HorizontalResolution, oldbitmap.VerticalResolution);

			g = System.Drawing.Graphics.FromImage(neu2);
			g.InterpolationMode = InterpolationMode.HighQualityBicubic;
			g.CompositingQuality = CompositingQuality.HighQuality;
			g.SmoothingMode = SmoothingMode.HighQuality;
			g.DrawImage(neu, new Rectangle(0, 0, nw, nh), new Rectangle(0, 0, neu.Width, neu.Height), GraphicsUnit.Pixel);
		


			     return neu2;
		}	   
	}
}





